<?php
	
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_USER.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_USER,$login['staff_auth']);

$dosearch = FALSE ;

$dao = new Class_M_USER;


if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['USER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['USER'] = $forms;
}

$wherearr =  array();

if ( isset( $forms['user_name'] ) &&  ( !empty( $forms['user_name'] ) ) ){
	$wherearr[] = sprintf(" user_name like  '%s' ",  "%".$dao->esc( $forms['user_name'] )."%" );
}

if ( isset( $forms['auth'] ) &&  ( is_numeric( $forms['auth'] ) ) ){
	$wherearr[] = sprintf(" auth = %s ",  $forms['auth'] );
}


if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1 );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2 );
}
 

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "user".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	

	$colums = array();
	/*
	 * login_id	ログインID
		pwd 	パスワード
		user_name	ユーザー名
		auth	権限
		auth_name 権限名
		ins_co_cd	折込会社コード
		ins_co_name 折込会社名
		email	メールアドレス
		status 状態
		memo	備考
		//login_ng	ログイン不可
		u_time	更新日時
		u_user	更新者名
		c_time	新規日時
c_user	新規者名
	 */
	 //1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'login_id'      => array( 'ログインID',     1),
		'login_pwd'     => array( 'パスワード',     1),
		'user_name'     => array( 'ユーザー名',     1),
		'auth'          => array( '権限',           2),
		'auth_name'     => array( '権限名',         1),
		'ins_co_cd'     => array( '折込会社コード', 1),
		'ins_co_name'   => array( '折込会社名',     1),
		//'electric_id'   => array( '電波圏ID',     1),
		//'electric_name' => array( '放送局',     1),
		'email'         => array( 'メールアドレス', 1),
		'status'        => array( '状態',           2),
		'memo'          => array( '備考',           4),
		'u_time'        => array( '更新日時',       3),
		'c_time'        => array( '登録日時',       3),
		'm_user_name'   => array( '更新者',         1)
	);

	

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}

	

	$data = $dao->search_csv( $wherearr );
	$ins_co = Class_M_OTHER::get_ins_co_array();
	//データ整形開始
	foreach ($data as $k => $v) {
		//$colums["type"][$k] = $v[1];
		$data[$k]["auth_name"]   = $arr_auth[$v["auth"]];
		//$data[$k]["ins_co_name"] = $ins_co[$v["ins_co_cd"]];
		$data[$k]["status"]      =  $v['login_ng'] == '1' ? 1:0;
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了

if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
	$st = ( $page['current'] -1 ) * PAGE_MAX;
}else{
	$page['current'] = 1;
	$st = 0;
}

$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
}


$select = array(''=>"選択してください");
$arr_auth = array_merge($select, $arr_auth);

if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) ) $smarty->assign('data', $data);
if( isset($page) ) $smarty->assign('page', $page);
$smarty->assign('auth', $arr_auth );
$ins_co = Class_M_OTHER::get_ins_co_array();
$smarty->assign('ins_co', $ins_co );

$electric = Class_M_OTHER::get_electric_array();
$smarty->assign('electric', $electric );


$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('user_index.html');

?>